<template>
	<view class="add">
		 <form @submit="onSubmit">  
		  <view class="pic">
			<text>零食图片</text>
			<uni-file-picker 
			   :auto-upload="false"
				v-model="imageValue" 
				file-extname="jpg,png,gif,webp"
				mode="grid"
				limit="1"
				:image-styles="imageStyles"
				@select="success"
			/>
		</view>
		<view class="uni-form-item" style="display: flex;">
						<text class="label">零食名称</text>
						<input class="uni-input"  placeholder=""  v-model="name"/>
		</view>
		<view class="uni-form-item" style="display: flex;">
						<text class="label">零食单价</text>
						<input class="uni-input"  placeholder="" v-model="price"/>
		</view>
		<view class="uni-form-item" style="display: flex;">
						<text class="label">分类</text>
						<input class="uni-input"  placeholder=""  v-model="type"/>
		</view>
		<button @click="upload">零食上新</button>
		</form>  
		
		<input class="uni-input"  placeholder=""  v-model="snack_id"/>
		<input class="uni-input"  placeholder=""  v-model="recommend_type"/>
		<button @click="upload2(snack_id,recommend_type)">零食推荐</button>
	</view>
</template>

<script>
	const cloudsnacks=uniCloud.importObject("snacks")
	const cloudsnacks2=uniCloud.importObject("recomment")
	export default {
		data() {
			return {
				snack_id:null,
				recommend_type:"",
				imageValue:[],
				imgUrl:"",
				imgName:"",
				name:"",
				price:'',
				type:"",
				imageStyles:{
							width:150,
							height:150,
							},
			};
		},
		methods:{
			 upload2(a,b){
				 cloudsnacks2.add(a,b)
			 },
				async upload(){
					let res = await uniCloud.uploadFile({
						filePath: this.imgUrl,
						cloudPath: this.imgName
					})
					console.log(res);
				      cloudsnacks.add({
						imageValue:res.fileID,
						name:this.name,
                        price:this.price,
					    type:this.type,
						create_date:Date.now(),
					}).then(res=>{
						console.log(res);
					})
					
				},
				success(e){
					console.log(e);
					this.imgUrl=e.tempFiles[0].path;
					this.imgName=e.tempFiles[0].name;
				}
	},
}
</script>

<style lang="scss">
.add{
	width: 100%;
	.pic{
		display: flex;
	}
	.uni-input{
		border: 1px solid #666;
	}
	
}
</style>
